package cn.tedu;
import com.mysql.cj.jdbc.Driver;
import com.sun.deploy.uitoolkit.impl.awt.AWTDragHelper;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
public class JdbcDome4 {
    public static void main(String[] args) throws Exception{
        ArrayList<EMP> list=new ArrayList<>();
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取数据通道
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/db40?characterEncoding=utf-8&serverTimezone=Asia/Shanghai", "root", "root");
        //创建数据传输工具
        Statement statement = connection.createStatement();
        //通过数据传输工具对数据库进行增删改查
       /*添加*/ statement.executeUpdate("insert into emp select 10090,'白家瑞','CEO',null,'1996-05-05',99999,null,null from dual " +
                "where not exists (select*from emp where id=10090)");
       /*删除*/ statement.executeUpdate("delete from emp where id=1001");
       /*修改*/ statement.executeUpdate("update emp set sal=sal-8000 where id=1003");
       /*查询*/
        ResultSet resultSet = statement.executeQuery("select*from emp");
        while (resultSet.next()){
            int id=resultSet.getInt("id");
            String name=resultSet.getString("name");
            String job=resultSet.getString("job");
            int topid=resultSet.getInt("topid");
            Date date = resultSet.getDate("hdate");
            int sal=resultSet.getInt("sal");
            int bonus=resultSet.getInt("bonus");
            int dept_id=resultSet.getInt("dept_id");
            EMP emp=new EMP(id,name,job,topid,date,sal,bonus,dept_id);
            list.add(emp);
        }
        resultSet.close();
        statement.close();
        connection.close();
        Iterator<EMP> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }

    }
}
